﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace iSciences.Data
{
    [DataContract(IsReference = true)]
    public class Article
    {
        Guid uniqueId;
        [DataMember]
        public Guid UniqueId
        {
            get { return uniqueId; }
            set { uniqueId = value; }
        }

        string title;
        [DataMember]
        public string Title
        {
            get { return title.Trim(); }
            set { title = value; }
        }

        string subtitle;
        [DataMember]
        public string Subtitle
        {
            get { return subtitle.Trim(); }
            set { subtitle = value; }
        }

        string url;
        [DataMember]
        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        string pubDate;
        [DataMember]
        public string PubDate
        {
            get { return pubDate; }
            set { pubDate = value; }
        }

        string imageUrl;
        [DataMember]
        public string ImageUrl
        {
            get { return imageUrl != null ? imageUrl : AppData._baseUri + "Assets/noimage.png"; }
            set { imageUrl = value; }
        }

        private ImageSource image;
        public ImageSource Image
        {
            get
            {
                try
                {
                    BitmapImage img = new BitmapImage(new Uri(imageUrl));
                    img.DecodePixelHeight = 250;
                    img.DecodePixelWidth = 250;
                    return img;
                }
                catch (Exception)
                {
                    BitmapImage img = new BitmapImage(new Uri(AppData._baseUri, "Assets/noimage.png"));
                    img.DecodePixelHeight = 250;
                    img.DecodePixelWidth = 250;
                    return img;
                }
            }
            set { this.image = value; }
        }

        string content;
        [DataMember]
        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        string author;
        [DataMember]
        public string Author
        {
            get { return author; }
            set { author = value; }
        }

        private Category category;
        [DataMember]
        public Category Parent
        {
            get { return category; }
            set { category = value; }
        }

        public Article(Guid uniqueId, string title, string subtitle, string pubdate, string author, string imageUrl, string url, string content, Category category)
        {
            this.uniqueId = uniqueId;
            this.title = title;
            this.subtitle = subtitle;
            this.pubDate = pubdate;
            this.author = author;
            this.imageUrl = imageUrl;
            this.url = url;
            this.content = content;
            this.category = category;
        }

        public Article()
        {
            this.uniqueId = Guid.NewGuid();
        }
    }
}
